/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.bg;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.aphrodite.image.ImageScaling;

/**
 * <p>
 * Use an image as a background for a widget.
 * </p>
 * If the image dimensions do not match that of the widget, the image is resized
 * 
 * @author kentelt
 * 
 */
public class ImageBackground extends Background {
	private Image imgBG;

	/**
	 * 
	 * @param bgImage
	 */
	public ImageBackground(Image bgImage) {
		// super(0, 0, bgImage.getWidth(), bgImage.getHeight(), true);
		super();
		this.imgBG = bgImage;
	}

	public void cleanUp() {
		imgBG = null;

	}

	public void render(Graphics g) {
		g.drawImage(imgBG, x, y, 0);
	}

	public void resize(int width, int height) {
		super.resize(width, height);
		scale();
	}

	private void scale() {
		this.imgBG = ImageScaling.doBresenhamScale(imgBG, imgBG.getWidth(), this.height, true);
	}

	public void setHeight(int height) {
		this.height = height;
		scale();
	}

	public void setWidth(int width) {
		this.width = width;
		scale();
	}

}
